{
 "metadata": {
  "name": "",
  "signature": "sha256:892aff311217ecbcdfdc5fee3eff5e8d844bf4a6921442aaf32b6f064c3932c7"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "# Entropy maximization\n",
      "\n",
      "A derivative work by Judson Wilson, 6/2/2014.<br>\n",
      "Adapted from the CVX example of the same name, by Jo\u00eblle Skaf, 4/24/2008.\n",
      "\n",
      "## Introduction\n",
      "\n",
      "Consider the linear inequality constrained entropy maximization problem:\n",
      "    \\begin{array}{ll}\n",
      "    \\mbox{maximize}   & -\\sum_{i=1}^n x_i \\log(x_i) \\\\\n",
      "    \\mbox{subject to} & \\sum_{i=1}^n x_i = 1 \\\\\n",
      "                      & Fx \\succeq g,\n",
      "    \\end{array}\n",
      "where the variable is $x \\in \\mathbf{{\\mbox{R}}}^{n}$.   \n",
      "\n",
      "This problem can be formulated in CVXPY using the `entr` atom.\n",
      "\n",
      "## Generate problem data"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import cvxpy as cvx\n",
      "import numpy as np\n",
      "\n",
      "# Make random input repeatable. \n",
      "np.random.seed(0) \n",
      "\n",
      "# Matrix size parameters.\n",
      "n = 20\n",
      "m = 10\n",
      "p = 5\n",
      "\n",
      "# Generate random problem data.\n",
      "tmp = np.mat(np.random.rand(n, 1))\n",
      "A = np.mat(np.random.randn(m, n))\n",
      "b = A*tmp\n",
      "F = np.mat(np.random.randn(p, n))\n",
      "g = F*tmp + np.mat(np.random.rand(p, 1))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 1
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "## Formulate and solve problem"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Entropy maximization.\n",
      "x = cvx.Variable(shape=(n,1))\n",
      "obj = cvx.Maximize(cvx.sum(cvx.entr(x)))\n",
      "constraints = [A*x == b,\n",
      "               F*x <= g ]\n",
      "prob = cvx.Problem(obj, constraints)\n",
      "prob.solve(solver=cvx.CVXOPT, verbose=True)\n",
      "\n",
      "# Print result.\n",
      "print \"\\nThe optimal value is:\", prob.value\n",
      "print '\\nThe optimal solution is:'\n",
      "print x.value"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "     pcost       dcost       gap    pres   dres\n",
        " 0:  0.0000e+00 -7.5220e+00  2e+01  1e+00  1e+00\n",
        " 1: -6.0720e+00 -5.9875e+00  2e+00  1e-01  2e-01\n",
        " 2: -5.4688e+00 -5.5885e+00  4e-01  2e-02  5e-02\n",
        " 3: -5.4595e+00 -5.4889e+00  5e-02  2e-03  2e-02\n",
        " 4: -5.4763e+00 -5.4816e+00  1e-02  3e-04  7e-03\n",
        " 5: -5.4804e+00 -5.4809e+00  1e-03  4e-05  2e-03\n",
        " 6: -5.4809e+00 -5.4809e+00  3e-05  1e-06  4e-04\n",
        " 7: -5.4809e+00 -5.4809e+00  4e-07  1e-08  2e-05\n",
        " 8: -5.4809e+00 -5.4809e+00  4e-09  1e-10  3e-07\n",
        " 9: -5.4809e+00 -5.4809e+00  4e-11  1e-12  5e-09"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "Optimal solution found.\n",
        "\n",
        "The optimal value is: 5.48090148635\n",
        "\n",
        "The optimal solution is:\n",
        "[[ 0.43483319]\n",
        " [ 0.66111715]\n",
        " [ 0.49201039]\n",
        " [ 0.36030618]\n",
        " [ 0.38416629]\n",
        " [ 0.30283658]\n",
        " [ 0.41730232]\n",
        " [ 0.79107794]\n",
        " [ 0.76667302]\n",
        " [ 0.38292365]\n",
        " [ 1.2479328 ]\n",
        " [ 0.50416987]\n",
        " [ 0.68053832]\n",
        " [ 0.67163958]\n",
        " [ 0.13877259]\n",
        " [ 0.5248668 ]\n",
        " [ 0.08418897]\n",
        " [ 0.56927148]\n",
        " [ 0.50000248]\n",
        " [ 0.78291311]]\n"
       ]
      }
     ],
     "prompt_number": 2
    }
   ],
   "metadata": {}
  }
 ]
}